package edu.ncsu.csc.itrust.http;

import com.meterware.httpunit.Button;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebTable;
import edu.ncsu.csc.itrust.enums.TransactionType;

/**
 * Use Case 26
 * @author Thien Nguyen
 *
 */
public class LabProcedureUC26Test extends iTrustHTTPTest {
	
	protected void setUp() throws Exception{
		super.setUp();
		gen.clearAllTables();
		gen.standardData();
//		gen.uap1();
//		gen.hcp0();
//		gen.patient2();
//		gen.patient4();
//		gen.loincs();
//		gen.labProcedures();
	}
	
	public void testAddLabProcedure1() throws Exception {
		// login HCP
		WebConversation wc = login("9000000000", "pw");
		WebResponse wr = wc.getCurrentPage();
		assertEquals("iTrust - HCP Home", wr.getTitle());
		
		// click Document Office Visit
		wr = wr.getLinkWith("Document Office Visit").click();
		// choose patient
		WebForm patientForm = wr.getForms()[0];
		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
		patientForm.getButtons()[1].click();
		wr = wc.getCurrentPage();
		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
		
		// click on pre-existing date
		wr.getLinkWith("06/10/2007").click();
		wr = wc.getCurrentPage();
		assertEquals(ADDRESS + "auth/hcp-uap/editOfficeVisit.jsp?ovID=955", wr.getURL().toString());
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		
		// add patient instruction
		WebForm form = wr.getForms()[0];
		form.setParameter("addLabProcID", "10763-1");
		form.setParameter("addLabProcPriority", "2");
		Button b = form.getButtonWithID("findLabTech");
		b.click();
		form.setParameter("addLtId", "5000000100");
		form.getSubmitButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		//assertLogged(TransactionType.LT_EDIT, 5000000100L, 2L, "Office visit");
	}
	
	public void testEditLabProcedure1() throws Exception {
		// login HCP
		WebConversation wc = login("9000000000", "pw");
		WebResponse wr = wc.getCurrentPage();
		assertEquals("iTrust - HCP Home", wr.getTitle());
		assertLogged(TransactionType.HOME_VIEW, 9000000000L, 0L, "");
		
		// go to edit laboratory procedures enter PID 2
		wr = wr.getLinkWith("Laboratory Procedures").click();
		assertTrue(wr.getText().contains("Please Select a Patient"));
		WebForm patientForm = wr.getForms()[0];
		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
		patientForm.getButtons()[1].click();
		// view lab procedures
		wr = wc.getCurrentPage();
		// make sure we get the right lab procedure
		assertTrue(wr.getText().contains("Lab Procedures"));		
		WebTable wt = wr.getTableStartingWith("Lab Procedures");
		assertTrue(wt.getTableCell(2, 8).getText().contains("2007-07-20"));
		//System.out.println(wt.getTableCell(1, 2).getText());
		assertTrue(wt.getTableCell(2, 1).getText().contains("10763-1"));
		wr = wt.getTableCell(2, 11).getLinkWith("Update").click();
		assertEquals("iTrust - Update Lab Procedure", wr.getTitle());
		WebForm form = wr.getForms()[0];
		// update the procedure
//		form.setParameter("Status", "PENDING");
//		form.setParameter("Results", "Negative.");
//		form.setParameter("Commentary", "All is well.");
		form.getSubmitButtons()[0].click();
		// check the update
		wr = wc.getCurrentPage();
//		assertLogged(TransactionType.LAB_RESULTS_EDIT, 9000000000L, 2L, "");
		assertTrue(wr.getText().contains("Updated Laboratory Procedure"));
		wt = wr.getTableStartingWith("Lab Procedures");
//		assertTrue(wt.getTableCell(2, 3).getText().contains("PENDING"));
//		assertTrue(wt.getTableCell(2, 4).getText().contains("All is well."));
//		assertTrue(wt.getTableCell(2, 5).getText().contains("Negative"));
		// check log
		wr = wr.getLinkWith("Transaction Log").click();
//		assertLogged(TransactionType.LAB_RESULTS_VIEW, 9000000000L, 2L, "");
	}
	
	public void testRemoveLabProcedure1() throws Exception {
		// login HCP
		WebConversation wc = login("9000000000", "pw");
		WebResponse wr = wc.getCurrentPage();
		assertEquals("iTrust - HCP Home", wr.getTitle());
		assertLogged(TransactionType.HOME_VIEW, 9000000000L, 0L, "");
		
		// go to edit laboratory procedures enter PID 2
		wr = wr.getLinkWith("Laboratory Procedures").click();
		assertTrue(wr.getText().contains("Please Select a Patient"));
		WebForm patientForm = wr.getForms()[0];
		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
		patientForm.getButtons()[1].click();
		// view lab procedures
		wr = wc.getCurrentPage();
		// make sure we get the right lab procedure
		assertTrue(wr.getText().contains("Lab Procedures"));		
		WebTable wt = wr.getTableStartingWith("Lab Procedures");
		assertTrue(wt.getTableCell(2, 8).getText().contains("2007-07-20"));
		//System.out.println(wt.getTableCell(1, 2).getText());
		assertTrue(wt.getTableCell(2, 1).getText().contains("10763-1"));
//		wr = wt.getTableCell(2, 11).getLinkWith("Remove").click();
//		
//		// check the update
//		assertEquals("iTrust - Update Lab Procedure", wr.getTitle());
//		wr = wc.getCurrentPage();
//		assertLogged(TransactionType.LAB_RESULTS_EDIT, 9000000000L, 2L, "");
//		assertTrue(wr.getText().contains("Updated Laboratory Procedure"));
//		wt = wr.getTableStartingWith("Lab Procedures");
//		assertFalse(wt.getTableCell(2, 3).getText().contains("PENDING"));
//		assertFalse(wt.getTableCell(2, 4).getText().contains("All is well."));
//		assertFalse(wt.getTableCell(2, 5).getText().contains("Negative"));
//		// check log
//		wr = wr.getLinkWith("Transaction Log").click();
//		assertLogged(TransactionType.LAB_RESULTS_VIEW, 9000000000L, 2L, "");
	}
	
	public void testLabTechEditStatus1() throws Exception {
//		// login
//		WebConversation wc = login("5000000000", "pw");
//		WebResponse wr = wc.getCurrentPage();
//		assertEquals("iTrust - LT Home", wr.getTitle());
//		assertLogged(TransactionType.HOME_VIEW, 5000000000L, 0L, "");
//		
//		// view assigned lab procedures
//		wr = wr.getLinkWith("Assigned Laboratory Procedures").click();
//		assertEquals("iTrust - LT Assigned Lab Procedures", wr.getTitle());
//		
//		WebTable wt = wr.getTableStartingWith("Assigned Lab Procedures");
//		assertTrue(wt.getTableCell(1, 1).getText().contains("ID"));
//		//System.out.println(wt.getTableCell(1, 2).getText());
//		assertTrue(wt.getTableCell(1, 2).getText().contains("Code"));
//		
//		assertTrue(wt.getTableCell(2, 1).getText().contains("955"));
//		wr = wt.getTableCell(2, 6).getLinkWith("Record").click();
//		assertEquals("iTrust - Record Lab Procedure", wr.getTitle());
//		WebForm form = wr.getForms()[0];
//		// record the lab procedure
//		form.setParameter("Status", "RECEIVED");
//		form.getSubmitButtons()[0].click();
//		// check the update
//		wr = wc.getCurrentPage();
//		assertTrue(wr.getText().contains("Recorded Laboratory Procedure"));
//		wt = wr.getTableStartingWith("Assigned Lab Procedures");
//		assertTrue(wt.getTableCell(2, 3).getText().contains("RECEIVED"));
//		// check log
//		wr = wr.getLinkWith("Transaction Log").click();
//		//assertLogged(TransactionType.LAB_TECH_RECORD, 5000000000L, "");
	}
	
	public void testLabTechReportResults1() throws Exception {
//		// login
//		WebConversation wc = login("5000000000", "pw");
//		WebResponse wr = wc.getCurrentPage();
//		assertEquals("iTrust - LT Home", wr.getTitle());
//		assertLogged(TransactionType.HOME_VIEW, 5000000000L, 0L, "");
//		
//		// view assigned lab procedures
//		wr = wr.getLinkWith("Assigned Laboratory Procedures").click();
//		assertEquals("iTrust - LT Assigned Lab Procedures", wr.getTitle());
//		
//		WebTable wt = wr.getTableStartingWith("Assigned Lab Procedures");
//		assertTrue(wt.getTableCell(1, 1).getText().contains("ID"));
//		//System.out.println(wt.getTableCell(1, 2).getText());
//		assertTrue(wt.getTableCell(1, 2).getText().contains("Code"));
//		
//		assertTrue(wt.getTableCell(2, 1).getText().contains("955"));
//		wr = wt.getTableCell(2, 6).getLinkWith("Record").click();
//		assertEquals("iTrust - Record Lab Procedure", wr.getTitle());
//		WebForm form = wr.getForms()[0];
//		// report results
//		form.setParameter("Result", "25");
//		form.setParameter("Confidence Interval", "25");
//		form.getSubmitButtons()[0].click();
//		// check the update
//		wr = wc.getCurrentPage();
//		assertTrue(wr.getText().contains("Recorded Laboratory Procedure"));
//		wt = wr.getTableStartingWith("Assigned Lab Procedures");
//		assertTrue(wt.getTableCell(2, 3).getText().contains("RECEIVED"));
//		// check log
//		wr = wr.getLinkWith("Transaction Log").click();
//		//assertLogged(TransactionType.LAB_TECH_RECORD, 5000000000L, "");
	}
	
	public void testAddLabProcedure2() throws Exception {
		// login HCP
		WebConversation wc = login("9000000003", "pw");
		WebResponse wr = wc.getCurrentPage();
		assertEquals("iTrust - HCP Home", wr.getTitle());
		
		// click Document Office Visit
		wr = wr.getLinkWith("Document Office Visit").click();
		// choose patient
		WebForm patientForm = wr.getForms()[0];
		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
		patientForm.getButtons()[1].click();
		wr = wc.getCurrentPage();
		assertEquals(ADDRESS + "auth/hcp-uap/documentOfficeVisit.jsp", wr.getURL().toString());
		
		// click on pre-existing date
		wr.getLinkWith("06/10/2007").click();
		wr = wc.getCurrentPage();
		assertEquals(ADDRESS + "auth/hcp-uap/editOfficeVisit.jsp?ovID=955", wr.getURL().toString());
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		
		// add patient instruction
		WebForm form = wr.getForms()[0];
		form.setParameter("addLabProcID", "10763-1");
		form.setParameter("addLabProcPriority", "2");
		Button b = form.getButtonWithID("findLabTech");
		b.click();
		form.setParameter("addLtId", "5000000100");
		form.getSubmitButtons()[0].click();
		wr = wc.getCurrentPage();
		assertEquals("iTrust - Document Office Visit", wr.getTitle());
		assertTrue(wr.getText().contains("Information Successfully Updated"));
		//assertLogged(TransactionType.OFFICE_VISIT_EDIT, 9000000000L, 2L, "Office visit");
	}
	
	public void testEditLabProcedure2() throws Exception {
		// login HCP
		WebConversation wc = login("9000000003", "pw");
		WebResponse wr = wc.getCurrentPage();
		assertEquals("iTrust - HCP Home", wr.getTitle());
		assertLogged(TransactionType.HOME_VIEW, 9000000003L, 0L, "");
		
		// go to edit laboratory procedures enter PID 2
		wr = wr.getLinkWith("Laboratory Procedures").click();
		assertTrue(wr.getText().contains("Please Select a Patient"));
		WebForm patientForm = wr.getForms()[0];
		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
		patientForm.getButtons()[1].click();
		// view lab procedures
		wr = wc.getCurrentPage();
		// make sure we get the right lab procedure
		assertTrue(wr.getText().contains("Lab Procedures"));		
		WebTable wt = wr.getTableStartingWith("Lab Procedures");
		assertTrue(wt.getTableCell(2, 8).getText().contains("2007-07-20"));
		//System.out.println(wt.getTableCell(1, 2).getText());
		assertTrue(wt.getTableCell(2, 1).getText().contains("10763-1"));
		wr = wt.getTableCell(2, 11).getLinkWith("Update").click();
		assertEquals("iTrust - Update Lab Procedure", wr.getTitle());
		WebForm form = wr.getForms()[0];
		// update the procedure
//		form.setParameter("Status", "PENDING");
//		form.setParameter("Results", "Negative.");
//		form.setParameter("Commentary", "All is well.");
		form.getSubmitButtons()[0].click();
		// check the update
		wr = wc.getCurrentPage();
//		assertLogged(TransactionType.LAB_RESULTS_EDIT, 9000000000L, 2L, "");
		assertTrue(wr.getText().contains("Updated Laboratory Procedure"));
		wt = wr.getTableStartingWith("Lab Procedures");
//		assertTrue(wt.getTableCell(2, 3).getText().contains("PENDING"));
//		assertTrue(wt.getTableCell(2, 4).getText().contains("All is well."));
//		assertTrue(wt.getTableCell(2, 5).getText().contains("Negative"));
		// check log
		wr = wr.getLinkWith("Transaction Log").click();
//		assertLogged(TransactionType.LAB_RESULTS_VIEW, 9000000000L, 2L, "");
	}
	
	public void testRemoveLabProcedure2() throws Exception {
		// login HCP
		WebConversation wc = login("9000000003", "pw");
		WebResponse wr = wc.getCurrentPage();
		assertEquals("iTrust - HCP Home", wr.getTitle());
		assertLogged(TransactionType.HOME_VIEW, 9000000003L, 0L, "");
		
		// go to edit laboratory procedures enter PID 2
		wr = wr.getLinkWith("Laboratory Procedures").click();
		assertTrue(wr.getText().contains("Please Select a Patient"));
		WebForm patientForm = wr.getForms()[0];
		patientForm.getScriptableObject().setParameterValue("UID_PATIENTID", "2");
		patientForm.getButtons()[1].click();
		// view lab procedures
		wr = wc.getCurrentPage();
		// make sure we get the right lab procedure
		assertTrue(wr.getText().contains("Lab Procedures"));		
		WebTable wt = wr.getTableStartingWith("Lab Procedures");
		assertTrue(wt.getTableCell(2, 8).getText().contains("2007-07-20"));
		//System.out.println(wt.getTableCell(1, 2).getText());
		assertTrue(wt.getTableCell(2, 1).getText().contains("10763-1"));
//		wr = wt.getTableCell(2, 11).getLinkWith("Remove").click();
//		
//		// check the update
//		assertEquals("iTrust - Update Lab Procedure", wr.getTitle());
//		wr = wc.getCurrentPage();
//		assertLogged(TransactionType.LAB_RESULTS_EDIT, 9000000000L, 2L, "");
//		assertTrue(wr.getText().contains("Updated Laboratory Procedure"));
//		wt = wr.getTableStartingWith("Lab Procedures");
//		assertFalse(wt.getTableCell(2, 3).getText().contains("PENDING"));
//		assertFalse(wt.getTableCell(2, 4).getText().contains("All is well."));
//		assertFalse(wt.getTableCell(2, 5).getText().contains("Negative"));
//		// check log
//		wr = wr.getLinkWith("Transaction Log").click();
//		assertLogged(TransactionType.LAB_RESULTS_VIEW, 9000000000L, 2L, "");
	}
	
}